<template>
  <div>
    <el-input v-model="searchText" placeholder="搜索" />
    <el-tree
        :expand-on-click-node="false"
        ref="tree"
        node-key="id"
        :data="dataTree"
        highlight-current
        :props="defaultProps"
        :default-expanded-keys="arr"
        :filter-node-method="filterNode"
        class="filter-tree"
        @node-click="node"
    />
  </div>
</template>
<script>
import { deptList } from '@/utils/dept';

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name:'List',
  props: {},
  data() {
    return {
      searchText: '',
      dataTree: [],
      arr:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  watch: {
    searchText(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getTreeData();
  },
  methods: {
    getTreeData() {
      deptList({}).then(res => {
        if (res.code === 1) {
          this.dataTree = [{
            id: 1,
            name: '淮南分公司',
            children: res.data
          }]
          this.dataTree.forEach((item) =>{
            this.arr.push(item.id)
          })

          console.log("this.arr:",this.arr)
        }
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      console.log('data.name.indexOf(value) !== -1', data.name.indexOf(value) !== -1)
      return data.name.indexOf(value) !== -1;
    },
    node(data) { //节点被点击的回调
      console.log(1111, data);
      this.$emit('nodeClick', data); //组件传值
    }
  }
}
</script>
<style lang="scss" scoped>
 ::v-deep .el-input__inner{
  width: 90%;
  margin: 0 auto;
}
.filter-tree{
  margin-top: 10px;
  ::v-deep .el-tree-node{
    width: 90%;
    margin: 0 auto;
  }
  ::v-deep .el-tree-node__content{
    height: 30px;
    font-size: 12px;
  }
}
</style>

